package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.ReturnScoreForm;
import com.example.emos.api.controller.form.SelectMarkBypagesForm;
import com.example.emos.api.controller.form.TaskForm;
import com.example.emos.api.controller.form.UpdateTimeForm;
import com.example.emos.api.service.MarkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author ：Supyo
 * @date ：Created in 2021/11/16 14:41
 * @description：
 */
@RestController
@RequestMapping("/mark")
@Tag(name = "MarkController", description = "考核任务接口")
@Slf4j
public class MarkController {

    @Autowired
    private MarkService markService;

    @PostMapping("/selectMarkBypages")
    @Operation(summary = "考核任务分页查询")
    public R selectMarkBypages(@Valid @RequestBody SelectMarkBypagesForm form) {
        int userId = StpUtil.getLoginIdAsInt();
        System.out.println("userId: " + userId);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        param.put("userId", userId);
        PageUtils pageUtils = markService.selectMarkBypages(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/task")
    @Operation(summary = "考核任务打分")
    public R task(@Valid @RequestBody TaskForm form) {
        HashMap rows = markService.task(form);
        return R.ok().put("data", rows);
    }

    @PostMapping("/returnScore")
    @Operation(summary = "完成打分")
    public R returnScore(@Valid @RequestBody ReturnScoreForm form) {
        int rows = markService.returnScore(form);
        return R.ok().put("data", rows);
    }

    @PostMapping("/updateTime")
    @SaCheckPermission(value = {"ROOT", "HR"}, mode = SaMode.OR)
    @Operation(summary = "修改时间")
    public R updateTime(@Valid @RequestBody UpdateTimeForm form) {
        int rows = markService.updateTime(form);
        return R.ok().put("data", rows);
    }

    @GetMapping("/getTotalTask")
    @Operation(summary = "获取待审批的考核任务")
    public R getTotalTask() {
        int userId = StpUtil.getLoginIdAsInt();
        int count = markService.getTotalTask(userId);
        return R.ok().put("count", count);
    }
}
